package com.ruoyi.information.controller.back;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.information.domain.back.CompanyNeedBack;
import com.ruoyi.information.domain.back.SysUserBack;
import com.ruoyi.information.mapper.back.SysUserBackMapper;
import com.ruoyi.information.service.back.ICompanyNeedBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 企业需求Controller
 * 后台
 * @author ruoyi
 * @date 2023-05-05
 */
@RestController
@RequestMapping("/needBack")
public class CompanyNeedBackController extends BaseController {
    @Autowired
    private ICompanyNeedBackService companyNeedService;

    @Autowired
    private SysUserBackMapper sysUserMapper;

    /**
     * 查询企业需求列表
     */
    @RequiresPermissions("information:need:list")
    @GetMapping("/list")
    public TableDataInfo list(CompanyNeedBack companyNeed) {
        startPage();
        List<CompanyNeedBack> list = companyNeedService.selectCompanyNeedList(companyNeed);
        return getDataTable(list);
    }

    /**
     * 导出企业需求列表
     */
    @RequiresPermissions("information:need:export")
    @Log(title = "企业需求", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CompanyNeedBack companyNeed) {
        List<CompanyNeedBack> list = companyNeedService.selectCompanyNeedList(companyNeed);
        for (CompanyNeedBack need : list) {
            SysUserBack user = sysUserMapper.selectSysUserByUserId(need.getUserId());
            if (user != null){
                if (user.getPhonenumber()!=null&&user.getPhonenumber()!=""){
                    need.setPhone(Long.valueOf(user.getPhonenumber()));
                }
            }else {
                need.setPhone(need.getUserId());
            }
        }
        ExcelUtil<CompanyNeedBack> util = new ExcelUtil<CompanyNeedBack>(CompanyNeedBack.class);
        util.exportExcel(response, list, "企业需求数据");
    }

    /**
     * 获取企业需求详细信息
     */
    @RequiresPermissions("information:need:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(companyNeedService.selectCompanyNeedById(id));
    }

    /**
     * 新增企业需求
     */
    @RequiresPermissions("information:need:add")
    @Log(title = "企业需求", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CompanyNeedBack companyNeed) {
        companyNeed.setUserId(SecurityUtils.getUserId());
        return toAjax(companyNeedService.insertCompanyNeed(companyNeed));
    }

    /**
     * 修改企业需求
     */
    @RequiresPermissions("information:need:edit")
    @Log(title = "企业需求", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CompanyNeedBack companyNeed) {
        companyNeed.setUpdateBy(SecurityUtils.getLoginUser().getSysUser().getNickName());
        return toAjax(companyNeedService.updateCompanyNeed(companyNeed));
    }

    /**
     * 删除企业需求
     */
    @RequiresPermissions("information:need:remove")
    @Log(title = "企业需求", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(companyNeedService.deleteCompanyNeedByIds(ids));
    }
}
